package com.workflow.listener;

import com.gec.domain.Role;
import com.gec.domain.User;
import com.gec.service.abst.ReimService;
import com.gec.utils.MyWebUtils;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;

public class ReimLeaderTaskHandler implements TaskListener
{

    private final ReimService reimService;

    public ReimLeaderTaskHandler()
    {
        this.reimService = MyWebUtils.getBean(ReimService.class);
    }

    @Override
    public void notify(DelegateTask task)
    {
        //{1}获取 HttpSession 中的用户
        User user = MyWebUtils.getCurrentUser();
        //{2}获取流程实例ID, 任务ID
        String insID = task.getProcessInstanceId();
        String taskID = task.getId();
        //{3}获取当前用户角色名称 [主要角色]
        assert user != null;
        Role priRole = user.getPrimaryRole();
        String roleName = priRole.getRoleName();
        String assignee;
        String deptId = user.getDept().getId();
        if ("employee".equals(roleName))
        {
            //4.1-情况一: 你是员工
            //4.2-获取你当前部门的领导的 user.id
            assignee = reimService.getDeptLeader(deptId);
            System.err.println("=====================");
            System.out.println("[ReimLeaderTaskHandler] "+ assignee);
            System.err.println("=====================");
        }
        else
        {
            throw new RuntimeException("找不到办理人。");
        }
        //4. 设置办理人
        task.setAssignee(assignee);
        //5. 保存 leave 信息到 t_leave 表。
        int i = reimService.updateAssigneeAndTask(insID, taskID, assignee);

        System.out.println("[ " + i + " 条任务已更新 ]");
    }

}
